//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "RendistatoCalculator.h"
using namespace Cephei::QL::Instruments::Bonds;
#include <gen/QL/Instruments/VanillaSwap.h>
#include <gen/QL/Instruments/Bonds/RendistatoBasket.h>
#include <gen/QL/Indexes/Ibor/Euribor.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL::Indexes::Ibor;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::Bonds::CRendistatoCalculator::CRendistatoCalculator (Cephei::QL::Instruments::Bonds::IRendistatoBasket^ basket, Cephei::QL::Indexes::Ibor::IEuribor^ euriborIndex, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CRendistatoBasket^ _Cbasket;
    CEuribor^ _CeuriborIndex;
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
#ifdef HANDLE
        _phRendistatoCalculator = NULL;
#endif
        _Cbasket = safe_cast<CRendistatoBasket^> (basket);
        _Cbasket->Lock();
        boost::shared_ptr<QuantLib::RendistatoBasket>& _basket = static_cast<boost::shared_ptr<QuantLib::RendistatoBasket>&> (_Cbasket->GetShared ()); 
        _CeuriborIndex = safe_cast<CEuribor^> (euriborIndex);
        _CeuriborIndex->Lock();
        boost::shared_ptr<QuantLib::Euribor>& _euriborIndex = static_cast<boost::shared_ptr<QuantLib::Euribor>&> (_CeuriborIndex->GetShared ()); 
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()); 
        _ppRendistatoCalculator = new boost::shared_ptr<QuantLib::RendistatoCalculator> (new QuantLib::RendistatoCalculator ( _basket,  _euriborIndex,  _discountCurve ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbasket != nullptr) _Cbasket->Unlock();
        if (_CeuriborIndex != nullptr) _CeuriborIndex->Unlock();
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Cephei::QL::Instruments::Bonds::CRendistatoCalculator::CRendistatoCalculator (boost::shared_ptr<QuantLib::RendistatoCalculator>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRendistatoCalculator = NULL;
#endif
	_ppRendistatoCalculator = &childNative;
    
}
Cephei::QL::Instruments::Bonds::CRendistatoCalculator::CRendistatoCalculator (QuantLib::RendistatoCalculator& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phRendistatoCalculator = NULL;
#endif
	_ppRendistatoCalculator = new boost::shared_ptr<QuantLib::RendistatoCalculator> (&childNative);
    
    _RendistatoCalculatorOwner = owner;
    
}

Cephei::QL::Instruments::Bonds::CRendistatoCalculator::CRendistatoCalculator (CRendistatoCalculator^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRendistatoCalculator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppRendistatoCalculator = new boost::shared_ptr<QuantLib::RendistatoCalculator> (copy->GetShared());
        
    }
}
Cephei::QL::Instruments::Bonds::CRendistatoCalculator::CRendistatoCalculator (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRendistatoCalculator = NULL;
#endif
	if (!t->IsSubclassOf(CRendistatoCalculator::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::Bonds::CRendistatoCalculator::CRendistatoCalculator (QuantLib::Handle<QuantLib::RendistatoCalculator>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phRendistatoCalculator = &childNative;
	_ppRendistatoCalculator = &static_cast<boost::shared_ptr<QuantLib::RendistatoCalculator>>(childNative.currentLink());
    
    _RendistatoCalculatorOwner = owner;
}
Cephei::QL::Instruments::Bonds::CRendistatoCalculator::CRendistatoCalculator (QuantLib::Handle<QuantLib::RendistatoCalculator> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phRendistatoCalculator = &childNative;
	_ppRendistatoCalculator = &static_cast<boost::shared_ptr<QuantLib::RendistatoCalculator>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::Bonds::CRendistatoCalculator::CRendistatoCalculator (QuantLib::RendistatoCalculator childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRendistatoCalculator = NULL;
#endif
	_ppRendistatoCalculator = new boost::shared_ptr<QuantLib::RendistatoCalculator> (new QuantLib::RendistatoCalculator (childNative));
    
}
#endif

Cephei::QL::Instruments::Bonds::CRendistatoCalculator::~CRendistatoCalculator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppRendistatoCalculator != NULL)
    {
	    delete _ppRendistatoCalculator;
        _ppRendistatoCalculator = NULL;
    }
}
Cephei::QL::Instruments::Bonds::CRendistatoCalculator::!CRendistatoCalculator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppRendistatoCalculator != NULL)
    {
	    delete _ppRendistatoCalculator;
    }
}
QuantLib::RendistatoCalculator& Cephei::QL::Instruments::Bonds::CRendistatoCalculator::GetReference ()
{
    if (_ppRendistatoCalculator == NULL) throw REFNEW NativeNullException ();
	return **_ppRendistatoCalculator;
}
boost::shared_ptr<QuantLib::RendistatoCalculator>& Cephei::QL::Instruments::Bonds::CRendistatoCalculator::GetShared ()
{
    if (_ppRendistatoCalculator == NULL) throw REFNEW NativeNullException ();
	return *_ppRendistatoCalculator;
}
QuantLib::RendistatoCalculator* Cephei::QL::Instruments::Bonds::CRendistatoCalculator::GetPointer ()
{
    if (_ppRendistatoCalculator == NULL) throw REFNEW NativeNullException ();
	return &**_ppRendistatoCalculator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::RendistatoCalculator>& Cephei::QL::Instruments::Bonds::CRendistatoCalculator::GetHandle ()
{
	if (_phRendistatoCalculator == NULL)
	{
		_phRendistatoCalculator = new Handle<QuantLib::RendistatoCalculator> (*_ppRendistatoCalculator);
	}
	return *_phRendistatoCalculator;
}
#endif
bool Cephei::QL::Instruments::Bonds::CRendistatoCalculator::HasNative () 
{
	return (_ppRendistatoCalculator != NULL);
}

Double Cephei::QL::Instruments::Bonds::CRendistatoCalculator::Duration::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppRendistatoCalculator)->duration ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Instruments::Bonds::CRendistatoCalculator::Durations::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppRendistatoCalculator)->durations ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::IVanillaSwap^ Cephei::QL::Instruments::Bonds::CRendistatoCalculator::EquivalentSwap::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::VanillaSwap> _rv = (boost::shared_ptr<QuantLib::VanillaSwap>)(*_ppRendistatoCalculator)->equivalentSwap ( );   
        Cephei::QL::Instruments::CVanillaSwap^ _nrv = REFNEW Cephei::QL::Instruments::CVanillaSwap (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::Bonds::CRendistatoCalculator::EquivalentSwapDuration::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppRendistatoCalculator)->equivalentSwapDuration ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::Bonds::CRendistatoCalculator::EquivalentSwapLength::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppRendistatoCalculator)->equivalentSwapLength ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::Bonds::CRendistatoCalculator::EquivalentSwapRate::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppRendistatoCalculator)->equivalentSwapRate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::Bonds::CRendistatoCalculator::EquivalentSwapSpread::get ()
{
    try
    {
    	QuantLib::Spread _rv = (QuantLib::Spread)(*_ppRendistatoCalculator)->equivalentSwapSpread ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::Bonds::CRendistatoCalculator::EquivalentSwapYield::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppRendistatoCalculator)->equivalentSwapYield ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Instruments::Bonds::CRendistatoCalculator::SwapDurations::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppRendistatoCalculator)->swapDurations ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Instruments::Bonds::CRendistatoCalculator::SwapLengths::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppRendistatoCalculator)->swapLengths ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Instruments::Bonds::CRendistatoCalculator::SwapRates::get ()
{
    try
    {
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppRendistatoCalculator)->swapRates ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Instruments::Bonds::CRendistatoCalculator::SwapYields::get ()
{
    try
    {
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppRendistatoCalculator)->swapYields ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::Bonds::CRendistatoCalculator::Yield::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppRendistatoCalculator)->yield ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Instruments::Bonds::CRendistatoCalculator::Yields::get ()
{
    try
    {
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppRendistatoCalculator)->yields ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::Bonds::IRendistatoCalculator^ Cephei::QL::Instruments::Bonds::CRendistatoCalculator_Factory::Create (Cephei::QL::Instruments::Bonds::IRendistatoBasket^ basket, Cephei::QL::Indexes::Ibor::IEuribor^ euriborIndex, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve)
{
    return REFNEW CRendistatoCalculator ( basket,  euriborIndex,  discountCurve);
}
